package com.zx.idc.backend.gui.network;


import com.baomidou.mybatisplus.plugins.Page;
import com.zx.idc.backend.gui.common.BaseApi;
import com.zx.idc.backend.gui.common.Result;
import com.zx.idc.common.spring.SpringConfigUtil;
import com.zx.idc.common.util.FileUtils;
import com.zx.idc.common.util.StreamUtils;
import com.zx.idc.common.util.ZipUtils;
import com.zx.idc.ds.network.entity.NetCommunicationLog;
import com.zx.idc.ds.network.service.INetCommunicationLogService;
import com.zx.idc.ds.network.vo.NetCommunicationLogVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipOutputStream;

import static com.zx.idc.common.constants.DiskStoragePathConstant.COMMUNICATION_MANAGEMENT_DOWNLOAD_DIRECTORY;

/**
 * <p>
 * 数据通讯记录 前端控制器
 * </p>
 *
 * @author rxliuli
 * @since 2019-04-25
 */
@Controller
@RequestMapping("/api/netCommunicationLog")
public class NetCommunicationLogController extends BaseApi {
    @Autowired
    private INetCommunicationLogService iNetCommunicationLogService;

    @GetMapping("/query-page")
    @ResponseBody
    @RequiresPermissions("network-configuration:net-communication-log")
    public Result<Page<NetCommunicationLogVo>> queryPage(Page<NetCommunicationLogVo> page, NetCommunicationLogVo netCommunicationLogVo) {
        return success(iNetCommunicationLogService.queryPage(page, netCommunicationLogVo));
    }

    @PostMapping("/delete/batch")
    @ResponseBody
    @RequiresPermissions("network-configuration:net-communication-log:edit")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        String msg;
        Exception ex;
        try {
            return success(iNetCommunicationLogService.deleteFiles(ids));
        } catch (IOException e) {
            msg = "通讯设置删除文件异常";
            ex = e;
        }
        log.error(msg, ex);
        return error(msg);
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    @RequiresPermissions("network-configuration:net-communication-log:edit")
    public Result<Boolean> deleteOne(@PathVariable("id") Integer id) {
        String msg;
        Exception ex;
        try {
            return success(iNetCommunicationLogService.deleteFile(id));
        } catch (IOException e) {
            msg = "通讯设置删除文件异常";
            ex = e;
        }
        log.error(msg, ex);
        return error(msg);
    }

    @PostMapping("/download-files")
    @RequiresPermissions("network-configuration:net-communication-log")
    public void downloadFiles(@RequestBody List<String> ids, HttpServletResponse response) {
        //获取勾选的文件路径
        List<NetCommunicationLog> netCommunicationLogs = iNetCommunicationLogService.selectBatchIds(ids);
        //获取根目录
        String rootPath = FileUtils.checkDirPathEndSeparator(SpringConfigUtil.get(COMMUNICATION_MANAGEMENT_DOWNLOAD_DIRECTORY));
        ZipOutputStream out = null;
        try {
            String zipName = "通讯资源.zip";
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
            out = new ZipOutputStream(response.getOutputStream());
            for (NetCommunicationLog netCommunicationLog : netCommunicationLogs) {
                String filePath = netCommunicationLog.getFilePath();
                if (StringUtils.isNotEmpty(filePath)) {
                    filePath = rootPath + filePath;

                    ZipUtils.doZip(
                        new File(filePath),
                        out,
                        new StringBuilder()
                            .append(netCommunicationLog.getFileName())
                            .append("_")
                            .append(System.currentTimeMillis())
                            .toString()
                    );
                    response.flushBuffer();
                }
            }
        } catch (Exception e) {
            log.error("下载错误", e);
        } finally {
            StreamUtils.closeStream(out);
        }
    }

}

